package com.xy.system.source.act;

import com.xy.system.source.IDataSourceServer;
import com.xy.system.source.vo.DataSourceVO;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OracleAction implements IDataSourceServer {

    public String getServerName() {
        return "oracle.jdbc.driver.OracleDriver";
    }

    public void fillDbName(DataSourceVO cond) {
        //        jdbc:oracle:thin:@192.168.0.253:1521:ORCLCDB
        String url = cond.getUrl();
        String newUrl = url.replaceAll("\\\\", "/");
        cond.setUrl(newUrl);
        newUrl = newUrl.replaceAll("/", ":");
        String[] dbStr = newUrl.split(":");
        String name = dbStr[dbStr.length - 1];
        cond.setDbName(name);
    }

    @Override
    public List<String> getAllTableMap(Connection conn, DataSourceVO dataSource) {
        List<String> tableList = new ArrayList<>();
        Statement st = null;
        ResultSet rs2 = null;
        try {
            st = conn.createStatement();
            rs2 = st.executeQuery("select t.table_name  from user_tables t");
            while (rs2.next()) {
                tableList.add(rs2.getString(1).toLowerCase());
            }
            return tableList;
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (st != null) {
                try {
                    st.close();
                } catch (Exception e1) {

                }
            }
            if (rs2 != null) {
                try {
                    rs2.close();
                } catch (Exception e2) {

                }
            }
        }

        return null;
    }


    @Override
    public Map<String, String> getRemarkNameMap(Connection conn, DataSourceVO dataSource, String tableName) {
        Map<String, String> remarkMap = new HashMap<>();
        Statement st = null;
        ResultSet rs2 = null;
        try {
            st = conn.createStatement();
            StringBuilder sql = new StringBuilder();
            sql.append(" SELECT ");
            sql.append("    column_name AS \"column_name\" ");
            sql.append("    ,comments    AS \"column_comment\" ");
            sql.append(" FROM  user_col_comments ");
            sql.append(" WHERE table_name = '").append(tableName.toUpperCase()).append("' ");
            rs2 = st.executeQuery(sql.toString());
//            rs2 = st.executeQuery("select column_name \"column_name\",COMMENTS \"column_comment\" from user_col_comments  where table_name = '" + tableName.toUpperCase() + "'");
            while (rs2.next()) {
                remarkMap.put(rs2.getString(1).toLowerCase(), rs2.getString(2));
            }
            return remarkMap;
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (st != null) {
                try {
                    st.close();
                } catch (Exception e1) {

                }
            }
            if (rs2 != null) {
                try {
                    rs2.close();
                } catch (Exception e2) {

                }
            }
        }

        return null;
    }

}
